import { Body, Controller, Get, Post, Query, Req, UseFilters } from "@nestjs/common";
import { UserService } from "./user.service";
import { User } from "./entities/user.entity";
import { UpdateUserDto } from "./dto/update-user.dto";
import { CreateUserDto } from "./dto/create-user.dto";
import { HttpExceptionFilter } from "src/exception/http-exception.filter";
import { wrapperResponse } from "src/utils";

@Controller('/users') // 设置路由前缀为 /users
export class UserController {
    constructor(
        private readonly userService:UserService
    ){}

    @Get('/info')
    getUserInfo(@Req() request) {
        // request: jwt是通过我们给的用户名等信息生成的token，而request拿到的就是jwt中存储的用户信息
        return wrapperResponse(
            this.userService.findByUsername(request.user.username),
            '获取用户信息成功'
        )
    }

    @Get('/detail')
    @UseFilters(new HttpExceptionFilter)
    getDetail(@Query() { id }):Promise<User | null> {
        return wrapperResponse(
            this.userService.findOne(id),
            '获取用户详情成功'
        )
    }

    @Get('/list')
    @UseFilters(new HttpExceptionFilter)
    getAllUser(@Query() query):Promise<User[]> {
        return wrapperResponse(
            this.userService.findAll(query),
            '查询用户成功'
        )
    }
    @Post('/create')
    @UseFilters(new HttpExceptionFilter)
    createUser(@Body() body:CreateUserDto) {
        return wrapperResponse(
            this.userService.create(body),
            '创建用户成功'
        )
    }

    @Post('/update')
    @UseFilters(new HttpExceptionFilter)
    update(@Body() body:UpdateUserDto) {
        return wrapperResponse(
            this.userService.update(body),
            '更新用户成功'
        )
    }
    @Post('/del')
    @UseFilters(new HttpExceptionFilter)
    delete(@Body() body:{id:string}) {
        return wrapperResponse(
            this.userService.delete(body.id),
            '删除用户成功'
        )
    } 
    @Post('/resetPwd')
    @UseFilters(new HttpExceptionFilter)
    resetPwd(@Body() body:{id:string}) {
        return wrapperResponse(
            this.userService.resetPwd(body.id),
            '重置密码成功'
        )
    }
}